//
//  HeightViewController.m
//  Leo01
//
//  Created by 张旭 on 15/9/17.
//  Copyright (c) 2015年 张旭. All rights reserved.
//

#import "WeightViewController.h"

@interface WeightViewController ()



@end

@implementation WeightViewController
@synthesize selectRound;
@synthesize selectDecimal;
@synthesize pickerView;
@synthesize cancelButton;
@synthesize confirmButton;
@synthesize motherLabel;
@synthesize motherWindow;
@synthesize roundNumber;
@synthesize decimalNumber;
@synthesize numberFormatter;

@synthesize originalRoundString;

int originalRound;
-(void)viewWillAppear:(BOOL)animated
{
    if (originalRoundString==NULL||[originalRoundString isEqualToString:@"0"]||[originalRoundString isEqualToString:@""])
    {
        originalRound=30;
        selectRound=@"30";
    }
    else
    {
        if (originalRound>100)
        {
            originalRound=100;
            selectRound=@"100";
        }
        if (originalRound<10)
        {
            originalRound=10;
            selectRound=@"10";
        }

        //originalRoundString=[originalRoundString substringToIndex:originalRoundString.length];
        originalRound=[originalRoundString intValue];
        selectRound=[NSString stringWithFormat:@"%d",originalRound];
    }
    [pickerView selectRow:(originalRound-10) inComponent:0 animated:NO];

    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setFrame:[UIScreen mainScreen].bounds];
    [self initDataArray];
    [self initButtonActions];
    numberFormatter = [[NSNumberFormatter alloc] init];
    pickerView.dataSource=self;
    pickerView.delegate=self;
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}


-(void)initDataArray
{
    roundNumber = [[NSMutableArray alloc]initWithCapacity:5];
    decimalNumber = [[NSMutableArray alloc]initWithCapacity:5];
    for (int i = 0; i <= 90; i++) {
        NSNumber *num = [NSNumber numberWithInt:i+10];
        [roundNumber addObject:num];
    }
//    for (int i = 0; i < 10; i++) {
//        NSNumber *num = [NSNumber numberWithInt:i];
//        [decimalNumber addObject:num];
//    }
}

-(void)initButtonActions{
    [confirmButton addTarget:self action:@selector(submitAction) forControlEvents:UIControlEventTouchDown];
    [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchDown];
}

-(void)submitAction
{
    if (selectRound == nil) {
        selectRound = @"10";
    }
//    if (selectDecimal == nil) {
//        selectDecimal = @"0";
//    }
    if ([selectRound intValue]>100)
    {
        selectRound=@"100";
    }
    if ([selectRound intValue]<10)
    {
        selectRound=@"10";
    }
    

    //self.motherLabel.text = [NSString stringWithFormat:@"%@.%@kg", selectRound, selectDecimal];
    self.motherLabel.text = [NSString stringWithFormat:@"%@kg", selectRound];
    self.motherWindow.hidden=YES;
    
}
-(void)cancelAction
{
    self.motherWindow.hidden=YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0){
        return roundNumber.count;
    }
    else{
        return decimalNumber.count;
    }
    
}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    if(component == 0){
        return [numberFormatter stringFromNumber:[roundNumber objectAtIndex:row]];
    }
    else{
        return [numberFormatter stringFromNumber:[decimalNumber objectAtIndex:row]];
    }
}
// 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(component == 0){
        selectRound = [numberFormatter stringFromNumber:[roundNumber objectAtIndex:row]];
    }
    else{
        selectDecimal = [numberFormatter stringFromNumber:[decimalNumber objectAtIndex:row]];
    }
    NSLog(@"%@,%@",selectRound,selectDecimal);
}

@end
